﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace Grizzy_X
{
    //author: peirong
    public partial class LegendForm : Form
    {
        public static CPointLegend.PointStyle CurrentStyle = CPointLegend.PointStyle.filled_circle;//记录当前是点图例的圆、三角形、正方形
        public Bitmap preview;
        public CPointLegend tempLegend1 = new CPointLegend();
        public CLineLegend tempLegend2 = new CLineLegend();
        public CPolygonLegend tempLegend3 = new CPolygonLegend();
        public CTextLegend tempLegend4 = new CTextLegend();
        public Color tempColor = Color.Red;//颜色缺省值为红色

        public LegendForm()
        {
            InitializeComponent();
        }

        //第一次读入时的刷新
        public void firstRefresh()
        {
            //初始化
    /*        if (Controller.CurrentLayer.Layer_Type == CLayer.LayerType.CPointLayer)
            {
                tabControl1.SelectedTab = tabPage1;
                tabPage2.Tag = tabPage3.Tag = tabPage4.Tag = false;
                tempLegend1.initialize();
                RefreshPreview1(tempLegend1);
            }
            else if (Controller.CurrentLayer.Layer_Type == CLayer.LayerType.CLineLayer)
            {
                tabControl1.SelectedTab = tabPage2;
                tabPage1.Tag = tabPage3.Tag = tabPage4.Tag = false;
                tempLegend2.initialize();
                RefreshPreview2(tempLegend2);
            }
            else if (Controller.CurrentLayer.Layer_Type == CLayer.LayerType.CPolygonLayer)
            {
                tabControl1.SelectedTab = tabPage3;
                tabPage1.Tag = tabPage2.Tag = tabPage4.Tag = false;
                tempLegend3.initialize();
                RefreshPreview3(tempLegend3);
            }
            else if (Controller.CurrentLayer.Layer_Type == CLayer.LayerType.CTextLayer)
            {
                tabControl1.SelectedTab = tabPage4;
                tabPage1.Tag = tabPage2.Tag = tabPage3.Tag = false;
                tempLegend4.initialize();
                RefreshPreview4(tempLegend4);
            }*/
        }

        CLegend curLegend;
        //根据要修改的图例来refresh
        public void newRefresh(int LegendIndex)
        {
            curLegend = Controller.MapLegends[LegendIndex];
            switch (curLegend.Legend_Type)
            {
                case CLegend.LegendType.CPointLegend:
                    {
                        tabControl1.SelectedTab = tabPage1;
                        tabPage2.Tag = tabPage3.Tag = tabPage4.Tag = false;
                        CPLegendTitleTextbox.Text = curLegend.Title;
                        CPLegendSubtitleTBox.Text = curLegend.Subtitle;
                        JudgePointStyle(((CPointLegend)curLegend));
                        CPLegendSizeTBox.Text = ((CPointLegend)curLegend).size.ToString();
                        textBox2.BackColor = ((CPointLegend)curLegend).color;
                        if (curLegend.Visible == true)
                            CPLegendVisibleCheckbox.Checked = true;
                        else CPLegendVisibleCheckbox.Checked = false;
                        if (curLegend.ByScale == true)
                            CPLegendByscaleCheckbox.Checked = true;
                        else CPLegendByscaleCheckbox.Checked = false;
                        RefreshPreview1(((CPointLegend)curLegend));
                        break;
                    }
                case CLegend.LegendType.CLineLegend:
                    {
                        tabControl1.SelectedTab = tabPage2;
                        tabPage1.Tag = tabPage3.Tag = tabPage4.Tag = false;
                        textBox5.Text = curLegend.Title;
                        textBox4.Text = curLegend.Subtitle;
                        if (((CLineLegend)curLegend).Line_Style == CLineLegend.LineStyle.solidline)
                            comboBox1.Text = "实线";
                        else
                            comboBox1.Text = "虚线";
                        textBox6.Text = ((CLineLegend)curLegend).line_weight.ToString();
                        textBox3.BackColor = ((CLineLegend)curLegend).color;
                        if (curLegend.Visible == true)
                            checkBox4.Checked = true;
                        else checkBox4.Checked = false;
                        if (curLegend.ByScale == true)
                            checkBox3.Checked = true;
                        else checkBox3.Checked = false;
                        RefreshPreview2(((CLineLegend)curLegend));
                        break;
                    }
                case CLegend.LegendType.CPolygonLegend:
                    {
                        tabControl1.SelectedTab = tabPage3;
                        tabPage1.Tag = tabPage2.Tag = tabPage4.Tag = false;
                        textBox8.Text = curLegend.Title;
                        textBox7.Text = curLegend.Subtitle;
                        if (((CPolygonLegend)curLegend).Border_Style == CPolygonLegend.BorderStyle.solidline)
                            comboBox2.Text = "实线";
                        else comboBox2.Text = "虚线";
                        if (((CPolygonLegend)curLegend).Fill_Style == CPolygonLegend.FillStyle.solid)
                            comboBox3.Text = "不透明";
                        else comboBox3.Text = "透明";
                        textBox12.BackColor = ((CPolygonLegend)curLegend).border_color;
                        textBox14.BackColor = ((CPolygonLegend)curLegend).fill_color;
                        if (curLegend.Visible == true)
                            checkBox6.Checked = true;
                        else checkBox6.Checked = false;
                        if (curLegend.ByScale == true)
                            checkBox5.Checked = true;
                        else checkBox5.Checked = false;
                        RefreshPreview3(((CPolygonLegend)curLegend));
                        break;
                    }
                case CLegend.LegendType.CTextLegend:
                    {
                        tabControl1.SelectedTab = tabPage4;
                        tabPage1.Tag = tabPage2.Tag = tabPage3.Tag = false;
                        textBox11.Text = curLegend.Title;
                        textBox10.Text = curLegend.Subtitle;
                        textBox15.BackColor = ((CTextLegend)curLegend).string_Color;
                        if (curLegend.Visible == true)
                            checkBox8.Checked = true;
                        else checkBox8.Checked = false;
                        if (curLegend.ByScale == true)
                            checkBox7.Checked = true;
                        else checkBox7.Checked = false;
                        RefreshPreview4(((CTextLegend)curLegend));
                        break;
                    }
            }
        }



        //判断修改尺寸时输入的尺寸是否有效（不含字符，只有数值）
        public bool isValidSize(string SizeText)
        {
            return (IsRegEx("^(-?[0-9]*[.]*[0-9]{0,3})$", SizeText));
        }

        private static bool IsRegEx(string regExValue, string itemValue)
        {
            try
            {
                Regex regex = new System.Text.RegularExpressions.Regex(regExValue);
                if (regex.IsMatch(itemValue)) return true;
                else return false;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
            }
        }

        //改变点图例的颜色
        private void button3_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog1 = new ColorDialog();
            if (colorDialog1.ShowDialog() == DialogResult.OK)
                tempColor = colorDialog1.Color;
            tempLegend1.color = tempColor;
            textBox2.BackColor = tempColor;
            RefreshPreview1(tempLegend1);
        }

        //改变点图例的样式
        private void StyleChoosingButton_Click(object sender, EventArgs e)
        {
            PointStyleForm pointStyleForm = new PointStyleForm();
            pointStyleForm.ShowDialog();
            tempLegend1.Point_Style = CurrentStyle;
            RefreshPreview1(tempLegend1);
            JudgePointStyle(tempLegend1);
        }

        //根据点类型来刷新文本框
        private void JudgePointStyle(CPointLegend tempLegend1)
        {
            if (tempLegend1.Point_Style == CPointLegend.PointStyle.circle)
                textBox1.Text = "圆";
            else if (tempLegend1.Point_Style == CPointLegend.PointStyle.filled_circle)
                textBox1.Text = "填充圆";
            else if (tempLegend1.Point_Style == CPointLegend.PointStyle.triangle)
                textBox1.Text = "三角形";
            else if (tempLegend1.Point_Style == CPointLegend.PointStyle.filled_triangle)
                textBox1.Text = "填充三角形";
            else if (tempLegend1.Point_Style == CPointLegend.PointStyle.rectangle)
                textBox1.Text = "正方形";
            else if (tempLegend1.Point_Style == CPointLegend.PointStyle.filled_rectangle)
                textBox1.Text = "填充正方形";
            else
                textBox1.Text = "圆";
        }

        //改变点图例的尺寸
        //注意！！！此处的尺寸是像素不是毫米！要改
        private void CPLegendSizeTBox_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(CPLegendSizeTBox.Text) && isValidSize(CPLegendSizeTBox.Text))
            {
                isChanged = true;
                tempLegend1.size = (float)(Convert.ToSingle(CPLegendSizeTBox.Text));
                RefreshPreview1(tempLegend1);
                if (Controller.WaitForChangeLegendIndex != -1 && status == "修改图例")
                {
                    ((CPointLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).size = (float)Convert.ToDouble(CPLegendSizeTBox.Text);
                }
            }
        }

        //改变线图例的线宽
        private void textBox6_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox6.Text) && isValidSize(textBox6.Text))
            {
                isChanged = true;
                tempLegend2.line_weight = (float)(Convert.ToSingle(textBox6.Text));
                if (Controller.WaitForChangeLegendIndex != -1 && status == "修改图例")
                {
                    ((CLineLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).line_weight = (float)Convert.ToDouble(textBox6.Text);
                }
                RefreshPreview2(tempLegend2);
            }
        }

        //改变线图例的颜色
        private void button4_Click(object sender, EventArgs e)
        {
            colorDialog2 = new ColorDialog();
            if (colorDialog2.ShowDialog() == DialogResult.OK)
                tempColor = colorDialog2.Color;
            tempLegend2.color = tempColor;
            textBox3.BackColor = tempColor;
            RefreshPreview2(tempLegend2);
        }

        //改变线图例的样式
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            isChanged = true;
            if (Controller.WaitForChangeLegendIndex != -1 && status == "修改图例")
            {
                switch (comboBox1.Text)
                {
                    case "实线":
                        tempLegend2.Line_Style = CLineLegend.LineStyle.solidline;
                        ((CLineLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).Line_Style = CLineLegend.LineStyle.solidline;
                        break;
                    case "虚线":
                        tempLegend2.Line_Style = CLineLegend.LineStyle.dashline;
                        ((CLineLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).Line_Style = CLineLegend.LineStyle.dashline;
                        break;
                }
            }
            RefreshPreview2(tempLegend2);
        }

        //改变多边形图例的边界线型
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            isChanged = true;
            if (Controller.WaitForChangeLegendIndex != -1 && status == "修改图例")
            {
                switch (comboBox2.Text)
                {
                    case "实线":
                        tempLegend3.Border_Style = CPolygonLegend.BorderStyle.solidline;
                        ((CPolygonLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).Border_Style = CPolygonLegend.BorderStyle.solidline;
                        break;
                    case "虚线":
                        tempLegend3.Border_Style = CPolygonLegend.BorderStyle.dashline;
                        ((CPolygonLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).Border_Style = CPolygonLegend.BorderStyle.dashline;
                        break;
                }
            }
            RefreshPreview3(tempLegend3);
        }

        //改变多边形图例的边界线宽度
        private void textBox12_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox12.Text) && isValidSize(textBox12.Text))
            {
                isChanged = true;
                tempLegend3.border_weight = (float)(Convert.ToSingle(textBox12.Text));
                if (Controller.WaitForChangeLegendIndex !=  -1 && status == "修改图例")
                {
                    ((CPolygonLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).border_weight = (float)Convert.ToDouble(textBox12.Text);
                }
                RefreshPreview3(tempLegend3);
            }
        }

        //改变多边形图例的边界颜色
        private void button7_Click(object sender, EventArgs e)
        {
            colorDialog3 = new ColorDialog();
            if (colorDialog3.ShowDialog() == DialogResult.OK)
                tempLegend3.border_color = colorDialog3.Color;
            textBox13.BackColor = colorDialog3.Color;
            RefreshPreview3(tempLegend3);
        }

        //改变多边形图例的填充类型
        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            isChanged = true;
            if (Controller.WaitForChangeLegendIndex != -1 && status == "修改图例")
            {
                switch (comboBox3.Text)
                {
                    case "不透明":
                        tempLegend3.Fill_Style = CPolygonLegend.FillStyle.solid;
                        ((CPolygonLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).Fill_Style = CPolygonLegend.FillStyle.solid;
                        break;
                    case "透明":
                        tempLegend3.Fill_Style = CPolygonLegend.FillStyle.transparent;
                        ((CPolygonLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).Fill_Style = CPolygonLegend.FillStyle.transparent;
                        break;
                }
            }
            RefreshPreview3(tempLegend3);
        }

        //改变多边形图例的填充颜色
        private void button5_Click(object sender, EventArgs e)
        {
            colorDialog4 = new ColorDialog();
            if (colorDialog4.ShowDialog() == DialogResult.OK)
                tempLegend3.fill_color = colorDialog4.Color;
            textBox14.BackColor = colorDialog4.Color;
            RefreshPreview3(tempLegend3);
        }

        //改变文本图例的样式
        private void button6_Click(object sender, EventArgs e)
        {
            FontDialog fontDialog1 = new FontDialog();
            if (fontDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox15.Font = fontDialog1.Font;
                tempLegend4.font = fontDialog1.Font;
            }
            RefreshPreview4(tempLegend4);
        }

        //文本框是否显示
        //目前暂时还未做！！！
        private void checkBox9_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox9.Checked)
            {
                groupBox8.Enabled = comboBox4.Enabled = button9.Enabled = comboBox5.Enabled = button10.Enabled = true;
            }
            else
                groupBox8.Enabled = comboBox4.Enabled = button9.Enabled = comboBox5.Enabled = button10.Enabled = false;
        }

        //文本颜色的选择
        private void button1_Click(object sender, EventArgs e)
        {
            colorDialog5 = new ColorDialog();
            if (colorDialog5.ShowDialog() == DialogResult.OK)
            {
                textBox15.BackColor = colorDialog5.Color;
                tempLegend4.string_Color = colorDialog5.Color;
            }
            RefreshPreview4(tempLegend4);
        }


        //刷新点图例的预览窗口
        public void RefreshPreview1(CPointLegend legend)
        {
            pictureBox1.Refresh();
            preview = new Bitmap((int)(legend.size) + 2, (int)(legend.size) + 2);//画笔默认为两个像素的宽度

            Graphics gra = Graphics.FromImage(preview);
            Graphics temp = pictureBox1.CreateGraphics();

            Pen pen = new Pen(legend.color, 2);
            SolidBrush brush = new SolidBrush(legend.color);
            Point[] points = new Point[4];
            //三角形坐标
            points[0].X = (int)(0.5 * legend.size); points[0].Y = (int)(legend.size  - Math.Sqrt(3) * 0.5 * legend.size);
            points[1].X = (int)(legend.size); points[1].Y = (int)(legend.size);
            points[2].X = 0; points[2].Y = (int)(legend.size);
            points[3] = points[0];
            float tempx;
            tempx = (float)(legend.size);
            if (tempLegend1.Point_Style == CPointLegend.PointStyle.circle)
                gra.DrawEllipse(pen, 0, 0, tempx, tempx);
            else if (tempLegend1.Point_Style == CPointLegend.PointStyle.filled_circle)
                gra.FillEllipse(brush, 0, 0, tempx, tempx);
            else if (tempLegend1.Point_Style == CPointLegend.PointStyle.rectangle)
                gra.DrawRectangle(pen, 0, 0, legend.size, legend.size);
            else if (tempLegend1.Point_Style == CPointLegend.PointStyle.filled_rectangle)
                gra.FillRectangle(brush, 0, 0, tempx, tempx);
            else if (tempLegend1.Point_Style == CPointLegend.PointStyle.triangle)
                gra.DrawLines(pen, points);
            else if (tempLegend1.Point_Style == CPointLegend.PointStyle.filled_triangle)
                gra.FillPolygon(brush, points);
            else
                gra.DrawEllipse(pen, 0, 0, tempx, tempx);
            temp.DrawImage(preview, pictureBox1.Width / 2 - tempx / 2, pictureBox1.Height / 2 - tempx / 2);
            legend.cpointlegend = preview;
           // pictureBox1.Image = preview;
        }

        //刷新线图例的预览窗口
        public void RefreshPreview2(CLineLegend legend)
        {
            preview = new Bitmap(pictureBox2.Width, pictureBox2.Height);
            Graphics gra = Graphics.FromImage(preview);
            Pen pen = new Pen(legend.color, (float)(legend.line_weight));

            if (legend.Line_Style == CLineLegend.LineStyle.solidline)
                pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
            else if (legend.Line_Style == CLineLegend.LineStyle.dashline)
            {
                float[] dashValues = { 3, 3 };
                pen.DashPattern = dashValues;
            }

            float start_X, start_Y, end_X, end_Y;
            start_X = 0;
            start_Y = (float)(0.5 * pictureBox2.Height);
            end_X = pictureBox2.Width;
            end_Y = (float)(0.5 * pictureBox2.Height);
            gra.DrawLine(pen, start_X, start_Y, end_X, end_Y);
            pictureBox2.Image = preview;
        }

        //刷新多边形图例的预览窗口
        public void RefreshPreview3(CPolygonLegend legend)
        {
            preview = new Bitmap(pictureBox3.Width, pictureBox3.Height);
            Graphics gra = Graphics.FromImage(preview);
            Pen pen = new Pen(legend.border_color, (float)(legend.border_weight));
            SolidBrush brush;

            if (legend.Border_Style == CPolygonLegend.BorderStyle.solidline)
                pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
            else if (legend.Border_Style == CPolygonLegend.BorderStyle.dashline)
            {
                float[] dashValues = { 3, 2 };
                pen.DashPattern = dashValues;
            }

            if (legend.Fill_Style == CPolygonLegend.FillStyle.solid)
                brush = new SolidBrush(legend.fill_color);
            else if (legend.Fill_Style == CPolygonLegend.FillStyle.transparent)
                brush = new SolidBrush(Color.Transparent);
            else brush = new SolidBrush(legend.fill_color);

            Point[] points = new Point[5];
            points[0].X = pictureBox3.Location.X + 10; points[0].Y = pictureBox3.Location.Y + 10;
            points[1].X = pictureBox3.Location.X + 5; points[1].Y = pictureBox3.Location.Y + 50;
            points[2].X = pictureBox3.Location.X + 80; points[2].Y = pictureBox3.Location.Y + 80;
            points[3].X = pictureBox3.Location.X + 150; points[3].Y = pictureBox3.Location.Y + 35;
            points[4].X = pictureBox3.Location.X + 10; points[4].Y = pictureBox3.Location.Y + 10;
            gra.DrawPolygon(pen, points);
            gra.FillPolygon(brush, points);
            pictureBox3.Image = preview;

        }

        //刷新文本图例的预览窗口
        public void RefreshPreview4(CTextLegend legend)
        {
            preview = new Bitmap(pictureBox3.Width, pictureBox3.Height);
            Graphics gra = Graphics.FromImage(preview);
            Pen pen = new Pen(legend.string_Color);
            SolidBrush brush = new SolidBrush(legend.string_Color);
            SizeF size = gra.MeasureString("Grizzy-X", legend.font);
            gra.DrawString("Grizzy-X", legend.font, brush, (float)(0.5 * pictureBox4.Width - size.Width / 2), (float)(0.5 * pictureBox4.Height - size.Height / 2));
            pictureBox4.Image = preview;
        }

        public string status = null;

        //点击确定按钮之后，系统记录下用户选择的图例信息
        private void legendOKbutton_Click_1(object sender, EventArgs e)
        {
            //lpr把“编辑”状态的开始设在Form1里面了！大家attention please~遇到问题告诉我
            //Controller.current_Operation = Controller.OperationStyles.Editing;
            //MainForm.pMainWin.EditButton();
            //当前图层为点图层，则建立新的点图例
            if (status == null)
            {
                if (Controller.CurrentLayer.Layer_Type == CLayer.LayerType.CPointLayer)
                {
                    tempLegend1.Title = CPLegendTitleTextbox.Text;
                    tempLegend1.Subtitle = CPLegendSubtitleTBox.Text;
                    tempLegend1.Point_Style = (CPointLegend.PointStyle)LegendForm.CurrentStyle;
                    if (CPLegendVisibleCheckbox.Checked == true)
                        tempLegend1.Visible = true;
                    else tempLegend1.Visible = false;
                    if (CPLegendByscaleCheckbox.Checked == true)
                        tempLegend1.ByScale = true;
                    else tempLegend1.ByScale = false;
                    if (tempLegend1.SetID(IDGenerator.MapLegendID()))
                    {
                        Controller.MapLegends.Add(tempLegend1);
                        Controller.CurrentLegendIndex = Controller.MapLegends.Count - 1;
                    }
                    else MessageBox.Show("图例ID生成错误！");
                    Controller.CurrentLegendIndex = Controller.MapLegends.Count - 1;
                }

                //如果当前图层为线图层，则建立新的线图例
                else if (Controller.CurrentLayer.Layer_Type == CLayer.LayerType.CLineLayer)
                {
                    tempLegend2.Title = textBox5.Text;
                    tempLegend2.Subtitle = textBox4.Text;
                    if (checkBox4.Checked == true)
                        tempLegend2.Visible = true;
                    else tempLegend2.Visible = false;
                    if (checkBox3.Checked == true)
                        tempLegend2.ByScale = true;
                    else tempLegend2.ByScale = false;
                    if (tempLegend2.SetID(IDGenerator.MapLegendID()))
                    {
                        Controller.MapLegends.Add(tempLegend2);
                        Controller.CurrentLegendIndex = Controller.MapLegends.Count - 1;
                    }
                    else MessageBox.Show("图例ID生成错误！");
                    Controller.CurrentLegendIndex = Controller.MapLegends.Count - 1;
                }

                else if (Controller.CurrentLayer.Layer_Type == CLayer.LayerType.CPolygonLayer)
                {
                    tempLegend3.Title = textBox8.Text;
                    tempLegend3.Subtitle = textBox7.Text;
                    if (checkBox6.Checked == true)
                        tempLegend3.Visible = true;
                    else tempLegend3.Visible = false;
                    if (checkBox5.Checked == true)
                        tempLegend3.ByScale = true;
                    else tempLegend3.ByScale = false;
                    if (tempLegend3.SetID(IDGenerator.MapLegendID()))
                    {
                        Controller.MapLegends.Add(tempLegend3);
                        Controller.CurrentLegendIndex = Controller.MapLegends.Count - 1;
                    }
                    else MessageBox.Show("图例ID生成错误！");
                    Controller.CurrentLegendIndex = Controller.MapLegends.Count - 1;

                }

                else if (Controller.CurrentLayer.Layer_Type == CLayer.LayerType.CTextLayer)
                {
                    tempLegend4.Title = textBox11.Text;
                    tempLegend4.Subtitle = textBox10.Text;
                    if (checkBox8.Checked == true)
                        tempLegend4.Visible = true;
                    else tempLegend4.Visible = false;
                    if (checkBox7.Checked == true)
                        tempLegend4.ByScale = true;
                    else tempLegend4.ByScale = false;
                    if (tempLegend4.SetID(IDGenerator.MapLegendID()))
                    {
                        Controller.MapLegends.Add(tempLegend4);
                        Controller.CurrentLegendIndex = Controller.MapLegends.Count - 1;
                    }
                    else MessageBox.Show("图例ID生成错误！");
                    Controller.CurrentLegendIndex = Controller.MapLegends.Count - 1;
                }

                else
                {
                    MessageBox.Show("请选择当前编辑图层！");
                }
            }
            if (status == "修改图例")
            {
                if (isChanged == true)//如果被修改了，则替换原有图例
                {
                    if (Controller.CurrentLayer.Layer_Type == CLayer.LayerType.CPointLayer)
                    {
                        if (Controller.WaitForChangeLegendIndex != -1)
                        {
                            Controller.MapLegends[Controller.WaitForChangeLegendIndex].Title = CPLegendTitleTextbox.Text;
                            Controller.MapLegends[Controller.WaitForChangeLegendIndex].Subtitle = CPLegendSubtitleTBox.Text;
                            ((CPointLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).Point_Style = (CPointLegend.PointStyle)LegendForm.CurrentStyle;
                        }
                        if (CPLegendVisibleCheckbox.Checked == true)
                            Controller.MapLegends[Controller.WaitForChangeLegendIndex].Visible = true;
                        else Controller.MapLegends[Controller.WaitForChangeLegendIndex].Visible = false;
                        if (CPLegendByscaleCheckbox.Checked == true)
                        Controller.MapLegends[Controller.WaitForChangeLegendIndex].ByScale = true;
                        else Controller.MapLegends[Controller.WaitForChangeLegendIndex].ByScale = false;
                        /*if (tempLegend1.SetID(IDGenerator.MapLegendID()))
                        {
                            Controller.MapLegends.Add(tempLegend1);
                            Controller.CurrentLegendIndex = Controller.MapLegends.Count - 1;
                        }
                        else MessageBox.Show("图例ID生成错误！");*/
                        Controller.CurrentLegendIndex = Controller.WaitForChangeLegendIndex;
                    }

            //如果当前图层为线图层，则建立新的线图例
                    else if (Controller.CurrentLayer.Layer_Type == CLayer.LayerType.CLineLayer)
                    {
                        if (Controller.WaitForChangeLegendIndex != -1)
                        {
                            Controller.MapLegends[Controller.WaitForChangeLegendIndex].Title = textBox5.Text;
                            Controller.MapLegends[Controller.WaitForChangeLegendIndex].Subtitle = textBox4.Text;

                            if (checkBox4.Checked == true)
                                Controller.MapLegends[Controller.WaitForChangeLegendIndex].Visible = true;
                            else Controller.MapLegends[Controller.WaitForChangeLegendIndex].Visible = false;
                            if (checkBox3.Checked == true)
                                Controller.MapLegends[Controller.WaitForChangeLegendIndex].ByScale = true;
                            else Controller.MapLegends[Controller.WaitForChangeLegendIndex].ByScale = false;
                        }
                       /* if (tempLegend2.SetID(IDGenerator.MapLegendID()))
                        {
                            Controller.MapLegends.Add(tempLegend2);
                            Controller.CurrentLegendIndex = Controller.MapLegends.Count - 1;
                        }
                        else MessageBox.Show("图例ID生成错误！");*/
                        Controller.CurrentLegendIndex = Controller.WaitForChangeLegendIndex;
                    }

                    else if (Controller.CurrentLayer.Layer_Type == CLayer.LayerType.CPolygonLayer)
                    {
                        if (Controller.WaitForChangeLegendIndex != -1)
                        {
                            Controller.MapLegends[Controller.WaitForChangeLegendIndex].Title = textBox8.Text;
                            Controller.MapLegends[Controller.WaitForChangeLegendIndex].Subtitle = textBox7.Text;

                            if (checkBox6.Checked == true)
                                Controller.MapLegends[Controller.WaitForChangeLegendIndex].Visible = true;
                            else Controller.MapLegends[Controller.WaitForChangeLegendIndex].Visible = false;
                            if (checkBox5.Checked == true)
                                Controller.MapLegends[Controller.WaitForChangeLegendIndex].ByScale = true;
                            else Controller.MapLegends[Controller.WaitForChangeLegendIndex].ByScale = false;
                        }
                        /*if (tempLegend3.SetID(IDGenerator.MapLegendID()))
                        {
                            Controller.MapLegends.Add(tempLegend3);
                            Controller.CurrentLegendIndex = Controller.MapLegends.Count - 1;
                        }
                        else MessageBox.Show("图例ID生成错误！");*/
                        Controller.CurrentLegendIndex = Controller.WaitForChangeLegendIndex;
                    }

                    else if (Controller.CurrentLayer.Layer_Type == CLayer.LayerType.CTextLayer)
                    {
                        if (Controller.WaitForChangeLegendIndex != -1)
                        {
                            Controller.MapLegends[Controller.WaitForChangeLegendIndex].Title = textBox11.Text;
                            Controller.MapLegends[Controller.WaitForChangeLegendIndex].Subtitle = textBox10.Text;
                            if (checkBox8.Checked == true)
                                Controller.MapLegends[Controller.WaitForChangeLegendIndex].Visible = true;
                            else Controller.MapLegends[Controller.WaitForChangeLegendIndex].Visible = false;
                            if (checkBox7.Checked == true)
                                Controller.MapLegends[Controller.WaitForChangeLegendIndex].ByScale = true;
                            else Controller.MapLegends[Controller.WaitForChangeLegendIndex].ByScale = false;
                        }
                        /*if (tempLegend4.SetID(IDGenerator.MapLegendID()))
                        {
                            Controller.MapLegends.Add(tempLegend4);
                            Controller.CurrentLegendIndex = Controller.MapLegends.Count - 1;
                        }
                        else MessageBox.Show("图例ID生成错误！");*/
                        Controller.CurrentLegendIndex = Controller.WaitForChangeLegendIndex;
                    }
                    if (MessageBox.Show("警告：图例已修改，会造成显示变化。") == DialogResult.OK)
                    {

                    }
                }
                status = null;//恢复非修改图例状态
                //修改了图例，需要重绘！
            }
            this.Close();
        }

        private void LegendForm_Load(object sender, EventArgs e)
        {
    
        }

        private void LegendForm_Shown(object sender, EventArgs e)
        {
            //初始化

            if (Controller.CurrentLayer.Layer_Type == CLayer.LayerType.CPointLayer)
            {
                tabControl1.SelectedTab = tabPage1;
                tabPage2.Tag = tabPage3.Tag = tabPage4.Tag = false;
                if (curLegend == null)
                    tempLegend1.initialize();
                else tempLegend1 = ((CPointLegend)curLegend);
                RefreshPreview1(tempLegend1);
            }

            else if (Controller.CurrentLayer.Layer_Type == CLayer.LayerType.CLineLayer)
            {
                tabControl1.SelectedTab = tabPage2;
                tabPage1.Tag = tabPage3.Tag = tabPage4.Tag = false;
                if (curLegend == null)
                    tempLegend2.initialize();
                else tempLegend2 = ((CLineLegend)curLegend);
                RefreshPreview2(tempLegend2);
            }
            else if (Controller.CurrentLayer.Layer_Type == CLayer.LayerType.CPolygonLayer)
            {
                tabControl1.SelectedTab = tabPage3;
                tabPage1.Tag = tabPage2.Tag = tabPage4.Tag = false;
                if (curLegend == null)
                    tempLegend3.initialize();
                else tempLegend3 = ((CPolygonLegend)curLegend);
                RefreshPreview3(tempLegend3);
            }
            else if (Controller.CurrentLayer.Layer_Type == CLayer.LayerType.CTextLayer)
            {
                tabControl1.SelectedTab = tabPage4;
                tabPage1.Tag = tabPage2.Tag = tabPage3.Tag = false;
                if (curLegend == null)
                    tempLegend4.initialize();
                else tempLegend4 = ((CTextLegend)curLegend);
                RefreshPreview4(tempLegend4);
            }
            
        }

       /* private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
        {
            if (!string.IsNullOrEmpty(e.TabPage.Tag.ToString()) && !Convert.ToBoolean(e.TabPage.Tag))
                e.Cancel = true;
        }*/

        //若点击“取消”，则没有图例，无法画图
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public bool isChanged = false;
        private void CPLegendTitleTextbox_TextChanged(object sender, EventArgs e)
        {
            isChanged = true;
            titleChanged = true;
            if (Controller.WaitForChangeLegendIndex != -1 && status == "修改图例")
            {
                ((CPointLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).Title = CPLegendTitleTextbox.Text;
            }
        }

        //修改了点图例的颜色
        private void textBox2_BackColorChanged(object sender, EventArgs e)
        {
            isChanged = true;
            if (Controller.WaitForChangeLegendIndex != -1 && status == "修改图例")
            {
                ((CPointLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).color = textBox2.BackColor;
            }
        }

        //修改了点图例的类型
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            isChanged = true;
            if (Controller.WaitForChangeLegendIndex != -1 && status == "修改图例")
            {
                switch (textBox1.Text)
                {
                    case "填充圆":
                        ((CPointLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).Point_Style = CPointLegend.PointStyle.filled_circle;
                        break;
                    case "圆":
                        ((CPointLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).Point_Style = CPointLegend.PointStyle.circle;
                        break;
                    case "填充三角形":
                        ((CPointLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).Point_Style = CPointLegend.PointStyle.filled_triangle;
                        break;
                    case "三角形":
                        ((CPointLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).Point_Style = CPointLegend.PointStyle.triangle;
                        break;
                    case "填充正方形":
                        ((CPointLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).Point_Style = CPointLegend.PointStyle.filled_rectangle;
                        break;
                    case "正方形":
                        ((CPointLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).Point_Style = CPointLegend.PointStyle.rectangle;
                        break;
                }
            }

        }

        private void CPLegendSubtitleTBox_TextChanged(object sender, EventArgs e)
        {
            isChanged = true;
            titleChanged = true;
            if (Controller.WaitForChangeLegendIndex != -1 && status == "修改图例")
            {
                ((CPointLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).Subtitle = CPLegendSubtitleTBox.Text;
            }
        }

        //修改线图例颜色
        private void textBox3_BackColorChanged(object sender, EventArgs e)
        {
            isChanged = true;
            if (Controller.WaitForChangeLegendIndex != -1 && status == "修改图例")
            {
                ((CLineLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).color = textBox3.BackColor;
            }
        }

        //线图例标题改变
        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            isChanged = true;
            titleChanged = true;
            if (Controller.WaitForChangeLegendIndex != -1 && status == "修改图例")
            {
                ((CLineLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).Title = textBox5.Text;
            }
        }

        //面图例边界颜色改变
        private void textBox13_BackColorChanged(object sender, EventArgs e)
        {
            isChanged = true;
            if (Controller.WaitForChangeLegendIndex != -1 && status == "修改图例")
            {
                ((CPolygonLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).border_color = textBox13.BackColor;
            }
        }
        //面图例填充色改变
        private void textBox14_BackColorChanged(object sender, EventArgs e)
        {
            isChanged = true;
            if (Controller.WaitForChangeLegendIndex != -1 && status == "修改图例")
            {
                ((CPolygonLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).fill_color = textBox14.BackColor;
            }
        }

        public bool titleChanged = false;

        //线图例副标题改变
        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            isChanged = true;
            titleChanged = true;
            if (Controller.WaitForChangeLegendIndex != -1 && status == "修改图例")
            {
                ((CLineLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).Subtitle = textBox4.Text;
            }
        }
        //面图例标题改变
        private void textBox8_TextChanged(object sender, EventArgs e)
        {
            isChanged = titleChanged = true;
            if (Controller.WaitForChangeLegendIndex != -1 && status == "修改图例")
            {
                ((CPolygonLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).Title = textBox8.Text;
            }
        }
        //面图例副标题改变
        private void textBox7_TextChanged(object sender, EventArgs e)
        {
            isChanged = titleChanged = true;
            if (Controller.WaitForChangeLegendIndex != -1 && status == "修改图例")
            {
                ((CPolygonLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).Subtitle = textBox7.Text;
            }
        }
        //文本标题改变
        private void textBox11_TextChanged(object sender, EventArgs e)
        {
            isChanged = titleChanged = true;
            if (Controller.WaitForChangeLegendIndex != -1 && status == "修改图例")
            {
                ((CTextLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).Title = textBox11.Text;
            }
        }
        //文本副标题
        private void textBox10_TextChanged(object sender, EventArgs e)
        {
            isChanged = titleChanged = true;
            if (Controller.WaitForChangeLegendIndex != -1 && status == "修改图例")
            {
                ((CTextLegend)Controller.MapLegends[Controller.WaitForChangeLegendIndex]).Subtitle = textBox10.Text;
            }
        }

        private void CPLegendByscaleCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            isChanged = true;
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            isChanged = true;
        }

        private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {
            isChanged = true;
        }

        private void checkBox7_CheckedChanged(object sender, EventArgs e)
        {
            isChanged = true;
        }
    }

}
